package com.frxuexi.apps.viewmodel

import android.util.Log
import androidx.lifecycle.viewModelScope
import com.frxuexi.apps.intent.AppsIntent
import com.frxuexi.apps.repository.AppsRepository
import com.frxuexi.lib_base.ApiResponse
import com.frxuexi.lib_base.BaseViewModel
import com.frxuexi.lib_base.UiState
import com.frxuexi.lib_base.download.DownloadState
import com.frxuexi.lib_base.download.DownloadUtil
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class AppsViewModel @Inject constructor(private val appsRepository: AppsRepository) :
    BaseViewModel<AppsIntent, UiState<Any>>() {

   /* private var _downState = MutableStateFlow<DownloadState>(DownloadState.onLoading)
    val downState: StateFlow<DownloadState>
        get() = _downState*/
    override fun handlerIntent(intent: AppsIntent) {
        when (intent) {
            is AppsIntent.getClientApps -> {
                httpRequest(appsRepository.getClientApps(intent.map) as Flow<ApiResponse<Any>>)
            }
            is AppsIntent.downloadApk->{
                downloadApk(intent.url,intent.path)
            }
            else -> {}
        }
    }




}